var _o2;
var cc__extends = __extends;
var cc__createBinding = __createBinding;
var cc__setModuleDefault = __setModuleDefault;
var cc__decorate = __decorate;
var cc__importStar = __importStar;
var cc__importDefault = __importDefault;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var cc__decorator = cc._decorator;
var ccp_ccclass = cc__decorator.ccclass;
var ccp_property = cc__decorator.property;
var $zqcc_OverallSystem = require("zqcc_OverallSystem");
var $1$CollectionItem = require("CollectionItem");
var f = cc__importStar($1$CollectionItem);
var $1$zqcc_pageBase = require("zqcc_pageBase");
var m = cc__importDefault($1$zqcc_pageBase);
var $1$gameConfig1 = require("gameConfig1");
var v = cc__importDefault($1$gameConfig1);
var def_CollectionPage = function (t) {
  function _ctor() {
    var cc_e = null !== t && t.apply(this, arguments) || this;
    cc_e.pageName = "收藏页面";
    cc_e.itemPrefab = null;
    cc_e.scrollView = null;
    cc_e.content = null;
    cc_e.countLabel = null;
    cc_e.columnCount = 4;
    cc_e.bufferRowCount = 1;
    cc_e.spacingX = 10;
    cc_e.spacingY = 10;
    cc_e.items = [];
    cc_e.itemPool = null;
    cc_e.totalCount = 0;
    cc_e.lastStartRow = -1;
    cc_e.itemHeight = 0;
    cc_e.itemWidth = 0;
    cc_e.itemTotalHeight = 0;
    cc_e.colWidth = 0;
    cc_e.itemStartX = 0;
    cc_e.selectedIndex = -1;
    cc_e.curLevel = 0;
    cc_e.allItems = [];
    return cc_e;
  }
  cc__extends(_ctor, t);
  _ctor.prototype.onLoad = function () {
    const currentContext = this; // 使用更具描述性的变量名
    const feibiaoData = $zqcc_OverallSystem.sers().userSrv.getFeibiaoData(); // 获取用户的肥表数据
    this.curLevel = feibiaoData.level; // 当前关卡级别
    const maxLevel = v.default.ROPE_MAX_LEVEL + 1; // 最大关卡级别
    let currentLevel = this.curLevel > maxLevel ? maxLevel : this.curLevel; // 限制当前关卡级别在最大关卡范围内

    // 更新UI显示
    this.countLabel.string = `${currentLevel}/${maxLevel}`;
    this.itemPool = new cc.NodePool(); // 初始化物体池
    this.scrollView.node.on("scrolling", this.onScrolling, this); // 绑定滚动事件
    this.initList(maxLevel); // 初始化列表

    // 获取选中的画作索引
    this.selectedIndex = $zqcc_OverallSystem.sers().userSrv.getSelectedPainting();
    cc.log("this.selectedIndex", this.selectedIndex);

    // 调度滚动和选中项的展示
    this.scheduleOnce(() => {
      currentContext.scrollToItem(currentContext.selectedIndex, true); // 滚动到选中的项
      this.scheduleOnce(() => {
        currentContext.onItemClick(currentContext.selectedIndex); // 点击选中的项
      }, 0.1);
    });
  };
  _ctor.prototype.initList = function (t) {
    this.totalCount = t; // 设置总项数
    this.content.removeAllChildren(); // 清空当前内容
    this.items = []; // 初始化项目数组

    // 实例化预制件以获取项的尺寸
    const itemInstance = cc.instantiate(this.itemPrefab);
    this.itemWidth = itemInstance.width; // 获取项的宽度
    this.itemHeight = itemInstance.height; // 获取项的高度
    this.itemTotalHeight = this.itemHeight + this.spacingY; // 计算项的总高度
    itemInstance.destroy(); // 销毁实例

    // 计算布局参数
    const totalSpacingX = this.spacingX * (this.columnCount - 1); // 计算总水平间距
    const totalWidth = this.itemWidth * this.columnCount; // 计算总宽度
    const offsetX = (this.content.width - (totalWidth + totalSpacingX)) / 2; // 计算偏移量
    this.itemStartX = -this.content.width / 2 + offsetX + this.itemWidth / 2; // 计算起始X坐标

    // 计算行数并设置内容高度
    const rowCount = Math.ceil(t / this.columnCount);
    this.content.height = rowCount * this.itemTotalHeight + this.spacingY; // 设置内容高度
    this.content.width = this.scrollView.node.width; // 设置内容宽度

    // 创建所有项目并更新它们
    this.createAllItems(rowCount);
    this.updateItems();

  };
  _ctor.prototype.createAllItems = function (t) {
    // 优化后的代码
    this.allItems = [];
    this.items = [];

    // 遍历所有项
    for (let rowIndex = 0; rowIndex < t; rowIndex++) {
      this.allItems[rowIndex] = []; // 初始化当前行的项数组

      for (let columnIndex = 0; columnIndex < this.columnCount; columnIndex++) {
        const itemIndex = rowIndex * this.columnCount + columnIndex; // 计算当前项的索引

        if (itemIndex >= this.totalCount) {
          break; // 超过总项数，结束当前行处理
        }

        const item = this.createOrGetItem(); // 创建或获取项
        const posX = this.itemStartX + columnIndex * (this.itemWidth + this.spacingX); // 计算X坐标
        const posY = -(rowIndex * (this.itemHeight + this.spacingY) + this.itemHeight / 2); // 计算Y坐标

        item.setPosition(posX, posY); // 设置项位置
        item.active = false; // 设置项不可见

        // 根据选中状态设置项状态
        let itemState = (itemIndex === this.selectedIndex) ? f.State.Selected : f.State.Normal;
        if (itemIndex >= this.curLevel) {
          itemState = f.State.Disabled; // 如果当前索引超过当前级别，设置为不可用
        }

        item.getComponent(f.default).updateItem(itemIndex, itemState); // 更新项的组件
        this.allItems[rowIndex][columnIndex] = item; // 将项添加到 allItems 中
        this.items.push(item); // 将该项添加到 items 数组
      }
    }
  };
  _ctor.prototype.updateItems = function () {
    // 计算起始行索引
    let startRowIndex = Math.max(0, Math.floor(Math.abs(this.content.y) / this.itemTotalHeight) - this.bufferRowCount);

    // 根据条件调整起始行索引
    if (this.content.y < 0) {
      startRowIndex = 0;
    }

    // 计算可见行数
    const visibleRowCount = Math.ceil(this.scrollView.node.height / this.itemTotalHeight);

    // 计算应显示的结束行索引
    const endRowIndex = Math.min(startRowIndex + visibleRowCount + this.bufferRowCount, Math.ceil(this.totalCount / this.columnCount));

    // 更新项的可视状态
    if (startRowIndex !== this.lastStartRow) {
      this.lastStartRow = startRowIndex; // 更新最后的起始行
      this.updateItemsVisibility(startRowIndex, endRowIndex); // 更新项的可视性
    }
  };
  _ctor.prototype.updateItemsVisibility = function (t, e) {
    // 隐藏不在可见范围内的项
    for (let i = 0; i < this.allItems.length; i++) {
      if (i < t || i > e) {
        const itemsInRow = this.allItems[i];
        if (itemsInRow) {
          itemsInRow.forEach(item => {
            if (item && item.active) {
              item.active = false;
            }
          });
        }
      }
    }

    // 显示在可见范围内的项
    for (let i = t; i <= e; i++) {
      const itemsInRow = this.allItems[i];
      if (itemsInRow) {
        itemsInRow.forEach(item => {
          if (item && !item.active) {
            item.active = true;
          }
        });
      }
    }
  };
  _ctor.prototype.createOrGetItem = function () {
    const context = this;  // 使用更具描述性的变量名
    const itemNode = this.itemPool.size() > 0 ? this.itemPool.get() : cc.instantiate(this.itemPrefab); // 获取或实例化项节点

    itemNode.parent = this.content; // 设置父节点
    itemNode.off(cc.Node.EventType.TOUCH_END); // 移除可能的事件绑定

    // 绑定触摸结束事件
    itemNode.on(cc.Node.EventType.TOUCH_END, function () {
      const itemIndex = itemNode.getComponent(f.default).getItemIndex(); // 获取项的索引
      context.onItemClick(itemIndex); // 调用点击处理
    }, context);

    return itemNode; // 返回创建的节点
  };
  _ctor.prototype.onItemClick = function (t) {
    const context = this; // 使用更具描述性的变量名
    const itemToSelect = this.items.find(item =>
      item.getComponent(f.default).getItemIndex() === t
    );

    if (itemToSelect && itemToSelect.getComponent(f.default).getState() !== f.State.Disabled) {
      this.selectedIndex = t; // 更新选中项索引
      cc.game.emit("changePainting", t); // 触发绘画更改事件

      const feibiaoData = $zqcc_OverallSystem.sers().userSrv.getFeibiaoData(); // 获取肥表数据
      feibiaoData.selectedPainting = this.selectedIndex; // 更新选中绘画
      $zqcc_OverallSystem.sers().userSrv.setFeibiaoData(feibiaoData); // 设置更新后的数据

      this.items.forEach(item => {
        const itemComponent = item.getComponent(f.default); // 获取组件
        const itemIndex = itemComponent.getItemIndex();
        if (itemComponent.getState() !== f.State.Disabled) {
          const state = (itemIndex === context.selectedIndex) ? f.State.Selected : f.State.Normal; // 确定状态
          itemComponent.setState(state); // 更新状态
        }
      });
    } else {
      $zqcc_OverallSystem.sers().uiSrv.showMessage("未解锁"); // 提示未解锁
    }
  };
  _ctor.prototype.onScrolling = function () {
    this.updateItems();
  };
  _ctor.prototype.scrollToItem = function (t, e) {
    if (undefined === e) {
      e = false; // 确保 e 的默认值为 false
    }

    const itemOffset = Math.floor(t / this.columnCount) * this.itemTotalHeight; // 计算项的偏移量
    const contentOverflow = this.content.height - this.scrollView.node.height; // 计算内容溢出高度
    const scrollToPosition = Math.min(contentOverflow, itemOffset); // 确定滚动到的位置

    // 执行滚动
    if (e) {
      this.scrollView.scrollToOffset(cc.v2(0, scrollToPosition)); // 无动画滚动
      this.updateItems(); // 更新项内容
    } else {
      this.scrollView.scrollToOffset(cc.v2(0, scrollToPosition), 0.3); // 带动画滚动
    }
  };
  cc__decorate([ccp_property(cc.Prefab)], _ctor.prototype, "itemPrefab", undefined);
  cc__decorate([ccp_property(cc.ScrollView)], _ctor.prototype, "scrollView", undefined);
  cc__decorate([ccp_property(cc.Node)], _ctor.prototype, "content", undefined);
  cc__decorate([ccp_property(cc.Label)], _ctor.prototype, "countLabel", undefined);
  cc__decorate([ccp_property], _ctor.prototype, "columnCount", undefined);
  cc__decorate([ccp_property], _ctor.prototype, "bufferRowCount", undefined);
  cc__decorate([ccp_property], _ctor.prototype, "spacingX", undefined);
  cc__decorate([ccp_property], _ctor.prototype, "spacingY", undefined);
  return cc__decorate([ccp_ccclass], _ctor);
}(m.default);
exports.default = def_CollectionPage;